package com.lfy.fyadmin.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lfy.fyadmin.common.R;
import com.lfy.fyadmin.entity.UserRole;
import com.lfy.fyadmin.service.UserRoleService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/user-role")
@Tag(name = "用户角色关联管理", description = "用户与角色关联关系接口")
public class UserRoleController {

    private final UserRoleService userRoleService;

    public UserRoleController(UserRoleService userRoleService) {
        this.userRoleService = userRoleService;
    }

    @PostMapping
    @Operation(summary = "新增用户角色关联")
    public R<Boolean> save(@RequestBody UserRole userRole) {
        boolean result = userRoleService.save(userRole);
        return result ? R.ok(true) : R.fail("新增失败");
    }

    @PutMapping
    @Operation(summary = "修改用户角色关联")
    public R<Boolean> update(@RequestBody UserRole userRole) {
        boolean result = userRoleService.updateSelective(userRole);
        return result ? R.ok(true) : R.fail("修改失败");
    }

    @DeleteMapping("/{id}")
    @Operation(summary = "删除用户角色关联")
    public R<Boolean> delete(@PathVariable Integer id) {
        boolean result = userRoleService.removeById(id);
        return result ? R.ok(true) : R.fail("删除失败");
    }

    @GetMapping("/{id}")
    @Operation(summary = "根据ID查询用户角色关联")
    public R<UserRole> getById(@PathVariable Integer id) {
        UserRole userRole = userRoleService.getById(id);
        return userRole != null ? R.ok(userRole) : R.fail("未找到关联");
    }

    @GetMapping("/page")
    @Operation(summary = "分页查询用户角色关联")
    public R<IPage<UserRole>> findPage(@RequestParam Integer pageNum,
                                       @RequestParam Integer pageSize) {
        Page<UserRole> page = new Page<>(pageNum, pageSize);
        return R.ok(userRoleService.page(page));
    }
}